Skip to content

Conversation

@jhlegarreta
Copy link
Contributor

Add gradient unit-sphere normalization function.

@codecov
Copy link

codecov bot commented Nov 20, 2025

Codecov Report

❌ Patch coverage is 95.61404% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.13%. Comparing base (d5fc6e1) to head (bb12af7).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
src/nifreeze/data/dmri.py 89.65% 2 Missing and 1 partial ⚠️
src/nifreeze/data/dmriutils.py 97.53% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #332      +/-   ##
==========================================
+ Coverage   78.87%   79.13%   +0.25%     
==========================================
  Files          28       29       +1     
  Lines        1884     1912      +28     
  Branches      203      204       +1     
==========================================
+ Hits         1486     1513      +27     
- Misses        333      339       +6     
+ Partials       65       60       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@oesteban
Copy link
Member

Any chance that we merge this into #327 and include it in the refactor?

@jhlegarreta
Copy link
Contributor Author

Any chance that we merge this into #327 and include it in the refactor?

In order to keep our attention on one feature at a time, avoid oversights and push things forward faster, I'd prefer to merge this as is. I will rebase #305 and #327 on main and resolve conflicts. Thanks.

@oesteban
Copy link
Member

oesteban commented Nov 21, 2025

I totally understand and I agree we should keep changes focused. And I also think we want to get the base object changes of #305 merged first, move relevant changes in that PR working on the dwi object into #327 and finally, merge this one into #327's branch. Effectively, the pipeline remains in the same philosophy while reducing developer and reviewer churn.

jhlegarreta and others added 5 commits November 21, 2025 12:15
Refactor gradient data checks in DWI data class: PR
nipreps#325 introduced the row-major
convention for gradient data in NiFreeze. However, gradient loading was
not tested thoroughly. This resulted in some execution flows that would
not guarantee a row-major internal convention or would crash under some
circumstances.

This commit refactors the gradient data checks and adds thorough
testing:
- Define all error or warning messages as global variables so that they
  can be checked exactly in tests.
- Ensure that gradients conform to the row-major convention when
  instantiating the DWI class directly. This allows to separate the
  gradient reformatting from the dimensionality check with the DWI
  volume sequence. This simplifies the flow, as the gradient
  reformatting to the row-major convention does not depend on the number
  of volumes in the DWI sequence. Also, this makes the flow more
  consistent with the refactored checks of the NIfTI file-based loading
  utility function (`from_nii`).
- Ensure that the gradients conform to the row-major convention
  immediately after loading the gradients file in the NIfTI file-based
  loading utility function (`from_nii`). As opposed to the previous
  implementation, this allows to load the gradients from a file where
  data follows either column-major or row-major convention. e.g. In the
  previous implementation the `if grad.shape[1] < 2:` was making an
  assumption about the layout and/or one that was wrong because we
  require 4 columns (direction (x,y,z) + b-value) or rows (if in
  column-major). The new implementation simplifies the execution flow.
This patch
- moves the responsibility of formatting and validating the
  gradients into the attrs' infrastructure;
- removes complexity from ``from_nifti()`` that was completely
  unnecessary (e.g., motion file can be set after creating the DWI
  object).
- updates tests
- skips one test that is unreasonably slow, we need to look into what's
  going on with ``to_nifti()``.
Improve gradient formatting function robustness by adopting a more
defensive approach:
- Raise if gradients are `None`.
- Raise if gradients are not a numeric homogeneous array-like object.

Add the corresponding tests.

Take advantage of the commit to test the `validate_gradients` function.
@oesteban
Copy link
Member

Can we change the merge base of this PR to #327 and get this merged there first? I think that would save a fair amount of time

Add gradient unit-sphere normalization function.
oesteban added a commit to jhlegarreta/nifreeze that referenced this pull request Nov 22, 2025
@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch from b5acdeb to a05bc94 Compare November 22, 2025 14:20
oesteban added a commit to jhlegarreta/nifreeze that referenced this pull request Nov 22, 2025
@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch from a05bc94 to d6b51f9 Compare November 22, 2025 14:23
oesteban added a commit to jhlegarreta/nifreeze that referenced this pull request Nov 22, 2025
@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch 2 times, most recently from 776f4df to d8975c5 Compare November 22, 2025 14:35
oesteban added a commit to jhlegarreta/nifreeze that referenced this pull request Nov 22, 2025
oesteban added a commit to jhlegarreta/nifreeze that referenced this pull request Nov 22, 2025
@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch from d8975c5 to 84d4c95 Compare November 22, 2025 14:39
oesteban added a commit to jhlegarreta/nifreeze that referenced this pull request Nov 22, 2025
@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch from 84d4c95 to 032f5f1 Compare November 22, 2025 15:17
@jhlegarreta
Copy link
Contributor Author

Thanks for having rebased this Oscar; reduces the complexity of what was in place previously. We are getting close.

@oesteban
Copy link
Member

Thanks for having rebased this Oscar; reduces the complexity of what was in place previously. We are getting close.

Still working on it, give me 20 min

@oesteban
Copy link
Member

Thanks for having rebased this Oscar; reduces the complexity of what was in place previously. We are getting close.

Still working on it, give me 20 min

I'll need some more time because the base branch is a pretty fast moving target... (with history rewrites, which makes the rebase shorter in number of commits but harder in their conflicts)

oesteban added a commit to jhlegarreta/nifreeze that referenced this pull request Nov 22, 2025
@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch from 032f5f1 to 6a81e9a Compare November 22, 2025 16:09
oesteban added a commit to jhlegarreta/nifreeze that referenced this pull request Nov 22, 2025
@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch from 6a81e9a to 3883853 Compare November 22, 2025 16:10
@oesteban
Copy link
Member

I'll need some more time because the base branch is a pretty fast moving target... (with history rewrites, which makes the rebase shorter in number of commits but harder in their conflicts)

Okay, I think it's best if I just make sure this one is in full green and I'll deal with the rebase tomorrow, so we can finally merge. Sorry, I can't pour more time into this today.

@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch from 9abe7a9 to 3883853 Compare November 22, 2025 16:15
@oesteban oesteban force-pushed the enh/normalize-gradients-unit-sphere branch from 3883853 to bb12af7 Compare November 22, 2025 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants